﻿

using System.Data.Common;
using System;
namespace QD.Entity
{
    public class ModelDataReader
    {
        DbDataReader reader = null;

        public ModelDataReader(DbDataReader _reader)
        {
            this.reader = _reader;
        }
        public static explicit operator ModelDataReader(DbDataReader reader)
        {
            ModelDataReader r = new ModelDataReader(reader);
            return r;
        }
        public T GetVal<T>(string key)
        {
            if (reader[key] == DBNull.Value) return default(T);
            Type type = typeof(T);
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                return (T)Convert.ChangeType(reader[key], Nullable.GetUnderlyingType(type));
            else
                return (T)Convert.ChangeType(reader[key], type);
        }
    }

}
